package avicit.cbb.techclass.bigsave.action;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;

import org.bson.types.ObjectId;

import avicit.platform6.fastdfs.utils.AESUtil;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

/**
 * <p>金航数码科技有限责任公司</p>
 * <p>作者：孟飞</p>
 * <p>邮箱：mengf@avicit.com</p>
 * <p>创建时间： 2014-10-8 下午2:44:42 </p>
 * <p>类说明：海量数据存储，使用MongoDB进行存储</p>
 * <p>修改记录： </p>
 */
public class MongoDBController {

	private String ip;
	private int port;
	private String dbname;
	private String username;
	private String password;
	private boolean isAuth;
	private String defaultCollection;
	private String keyfiled;
	private Map<String, String> collectionMap;

	private Mongo mongo = null;// mongo数据库连接对象(相当于connection)
	private DB db = null;// 数据库对象（要使用的数据库实例）
	private DBCollection dbc = null;// 数据库集合(相当于关系型数据库的表)
	private GridFS myFS = null;// 文件管理对象(数据库二进制文件管理对象)
	private Map<String, DBCollection> dbcMap = new HashMap<String, DBCollection>();
	private Map<String, GridFS> myFSMap = new HashMap<String, GridFS>();

	private boolean connFlag = false;
	private static int buffsize = 104857600;

	public MongoDBController(String ip, int port, String dbname, String username, String password, boolean isAuth, String defaultCollection, String keyfiled,
			Map<String, String> collectionMap) {
		this.ip = ip;
		this.port = port;
		this.dbname = dbname;
		this.username = username;
		this.password = password;
		this.isAuth = isAuth;
		this.defaultCollection = defaultCollection;
		this.keyfiled = keyfiled;
		this.collectionMap = collectionMap;

		this.getConnection();
	}

	/**
	 * 获取连接
	 *  void
	 * @throws
	 */
	public void getConnection() {
		try {
			this.mongo = new Mongo(this.ip, this.port);
			this.db = this.mongo.getDB(this.dbname);
			if (this.isAuth) {
				this.db.authenticate(this.username, this.password.toCharArray());
			}
			this.dbc = this.db.getCollection(this.defaultCollection + ".files");
			this.myFS = new GridFS(this.db, this.defaultCollection);

			if (this.collectionMap.size() > 0) {
				for (String modelName : this.collectionMap.keySet()) {
					String collectionName = this.collectionMap.get(modelName);
					DBCollection dbc = this.db.getCollection(collectionName + ".files");
					GridFS myFS = new GridFS(this.db, collectionName);

					this.dbcMap.put(modelName, dbc);
					this.myFSMap.put(modelName, myFS);
				}
			}

			this.connFlag = true;
		} catch (Exception e) {
			this.connFlag = false;
			e.printStackTrace();
		}
	}

	/**
	 * 判断是否连接
	 * @return boolean
	 * @throws
	 */
	public boolean isConnect() {
		try {
			this.db.command("show collections");
			return this.connFlag;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 插入文件操作
	 * @author 孟飞 
	 * @param inputStream
	 * @param filePathName
	 * @param id
	 * @return int
	 * @throws
	 */
	public int insertGridFS(InputStream inputStream, String filePathName, String id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.insertGridFS(inputStream, filePathName, map);
	}

	/**
	 * 插入文件操作
	 * @author 孟飞 
	 * @param inputStream
	 * @param filePathName
	 * @param map
	 * @return int
	 * @throws
	 */
	public int insertGridFS(InputStream inputStream, String filePathName, Map<String, String> map) {
		try {
			GridFSInputFile gridFSInputFile = myFS.createFile(inputStream, filePathName);
			gridFSInputFile.setChunkSize(1024 * 1024 * 2);// 设置每次-设置为2M这样可以减少生成的collection内document的数量

			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				gridFSInputFile.put(key, map.get(key));
			}

			gridFSInputFile.save();
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 插入文件操作
	 * @author 孟飞 
	 * @param inputStream
	 * @param filePathName
	 * @param id
	 * @return int
	 * @throws
	 */
	public int insertGridFS(InputStream inputStream, String filePathName, String id, String modelName) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.insertGridFS(inputStream, filePathName, map, modelName);
	}

	/**
	 * 插入文件操作
	 * @author 孟飞 
	 * @param inputStream
	 * @param filePathName
	 * @param map
	 * @return int
	 * @throws
	 */
	public int insertGridFS(InputStream inputStream, String filePathName, Map<String, String> map, String modelName) {
		if (this.myFSMap.get(modelName) == null) {
			return this.insertGridFS(inputStream, filePathName, map);
		}

		try {
			GridFSInputFile gridFSInputFile = this.myFSMap.get(modelName).createFile(inputStream, filePathName);
			gridFSInputFile.setChunkSize(1024 * 1024 * 2);// 设置每次-设置为2M这样可以减少生成的collection内document的数量

			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				gridFSInputFile.put(key, map.get(key));
			}

			gridFSInputFile.save();
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 更新文本信息操作
	 * @author 孟飞 
	 * @param mapOld
	 * @param mapNew
	 * @return int
	 * @throws
	 */
	public int updateDocumentById(String id, Map<String, ?> mapNew) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);

		return this.updateDocument(map, mapNew);
	}

	/**
	 * 更新文本信息操作
	 * @author 孟飞 
	 * @param mapOld
	 * @param mapNew
	 * @return int
	 * @throws
	 */
	public int updateDocument(Map<String, ?> mapOld, Map<String, ?> mapNew) {
		if (null == mapOld || null == mapNew) {
			return 0;
		}
		BasicDBObject updateCondition = new BasicDBObject();
		// 要更新的对象
		Iterator<String> it = mapOld.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			updateCondition.append(key, mapOld.get(key));
		}

		// 更新的新数据
		BasicDBObject updatedValue = new BasicDBObject();
		Iterator<String> itNew = mapNew.keySet().iterator();
		while (itNew.hasNext()) {
			String key = itNew.next();
			updatedValue.append(key, mapNew.get(key));
		}

		DBObject updateSetValue = new BasicDBObject("$set", updatedValue);
		int result = dbc.update(updateCondition, updateSetValue).getN();

		return result;
	}

	/**
	 * 更新文本信息操作
	 * @author 孟飞 
	 * @param mapOld
	 * @param mapNew
	 * @return int
	 * @throws
	 */
	public int updateDocumentById(String id, Map<String, ?> mapNew, String modelName) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);

		return this.updateDocument(map, mapNew, modelName);
	}

	/**
	 * 更新文本信息操作
	 * @author 孟飞 
	 * @param mapOld
	 * @param mapNew
	 * @return int
	 * @throws
	 */
	public int updateDocument(Map<String, ?> mapOld, Map<String, ?> mapNew, String modelName) {
		if (this.dbcMap.get(modelName) == null) {
			return this.updateDocument(mapOld, mapNew);
		}

		if (null == mapOld || null == mapNew) {
			return 0;
		}
		BasicDBObject updateCondition = new BasicDBObject();
		// 要更新的对象
		Iterator<String> it = mapOld.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			updateCondition.append(key, mapOld.get(key));
		}

		// 更新的新数据
		BasicDBObject updatedValue = new BasicDBObject();
		Iterator<String> itNew = mapNew.keySet().iterator();
		while (itNew.hasNext()) {
			String key = itNew.next();
			updatedValue.append(key, mapNew.get(key));
		}

		DBObject updateSetValue = new BasicDBObject("$set", updatedValue);
		int result = this.dbcMap.get(modelName).update(updateCondition, updateSetValue).getN();

		return result;
	}

	/**
	 * 保存文档到本地
	 * @author 孟飞 
	 * @param id
	 * @return InputStream
	 * @throws
	 */
	public InputStream getGridFSIOById(String id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.getGridFSIOById(map);
	}

	/**
	 * 从MongoDB保存文件到本地
	 * @author 孟飞 
	 * @param map
	 * @return InputStream
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public InputStream getGridFSIOById(Map map) {
		// 查询信息
		try {
			DBObject o = new BasicDBObject();
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				o.put(key, map.get(key));
			}

			GridFSDBFile gridFSFile = myFS.findOne(o);
			if (gridFSFile != null) {
				return gridFSFile.getInputStream();
			} else {
				return null;
			}
			// return gridFSFile.getInputStream(); // 这里有时候会报空指针错误 tip by wangjia
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 保存文档到本地
	 * @author 孟飞 
	 * @param id
	 * @return InputStream
	 * @throws
	 */
	public InputStream getGridFSIOById(String id, String modelName) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.getGridFSIOById(map, modelName);
	}

	/**
	 * 从MongoDB保存文件到本地
	 * @author 孟飞 
	 * @param map
	 * @return InputStream
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public InputStream getGridFSIOById(Map map, String modelName) {
		if (this.myFSMap.get(modelName) == null) {
			return this.getGridFSIOById(map);
		}

		// 查询信息
		try {
			DBObject o = new BasicDBObject();
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				o.put(key, map.get(key));
			}
			GridFSDBFile gridFSFile = this.myFSMap.get(modelName).findOne(o);
			return gridFSFile.getInputStream(); // 这里有时候会报空指针错误 tip by wangjia
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 保存文档到本地
	 * @author 孟飞 
	 * @param id
	 * @return List<GridFSDBFile>
	 * @throws
	 */
	public List<GridFSDBFile> getGridFSIOListsById(String id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.getGridFSIOListsById(map);
	}

	/**
	 * 从MongoDB保存文件到本地
	 * @author 孟飞 
	 * @param map
	 * @return InputStream
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public List<GridFSDBFile> getGridFSIOListsById(Map map) {
		// 查询信息
		try {
			DBObject o = new BasicDBObject();
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				o.put(key, map.get(key));
			}
			return myFS.find(o);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 保存文档到本地
	 * @author 孟飞 
	 * @param id
	 * @return List<GridFSDBFile>
	 * @throws
	 */
	public List<GridFSDBFile> getGridFSIOListsById(String id, String modelName) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.getGridFSIOListsById(map, modelName);
	}

	/**
	 * 从MongoDB保存文件到本地
	 * @author 孟飞 
	 * @param map
	 * @return InputStream
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public List<GridFSDBFile> getGridFSIOListsById(Map map, String modelName) {
		if (this.myFSMap.get(modelName) == null) {
			this.getGridFSIOListsById(map);
		}

		// 查询信息
		try {
			DBObject o = new BasicDBObject();
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				o.put(key, map.get(key));
			}
			return this.myFSMap.get(modelName).find(o);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 查询GridFS文件列表
	 * @author 孟飞 
	 * @param id
	 * @param sort
	 * @param value
	 * @return
	 * @throws Exception List<Map>
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public List<Map> queryGridFSById(String id, int sort, String value) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.queryGridFSByMap(map, sort, value);
	}

	/**
	 * 查询GridFS文件列表
	 * @author 孟飞 
	 * @param map
	 * @param sort
	 * @param value
	 * @return
	 * @throws Exception List<Map>
	 * @throws
	 */
	@SuppressWarnings({"rawtypes", "unchecked"})
	public List<Map> queryGridFSByMap(Map<String, ?> map, int sort, String value) throws Exception {

		BasicDBObject searchGridFS = new BasicDBObject();// 可选项-作为查询条件使用
		DBCursor cursor = null;
		if (null != map) {
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				if (key.equals("_id")) {
					searchGridFS.put(key, new ObjectId(map.get(key).toString()));
				} else {
					searchGridFS.append(key, map.get(key));
				}
			}
			cursor = myFS.getFileList(searchGridFS);
		} else {
			cursor = myFS.getFileList();
		}
		DBObject dbObject = new BasicDBObject();
		if (null != value && !"".equals(value) && (value.equals("uploadDate") || value.equals("filename"))) {
			dbObject.put(value, sort);
		}
		List<DBObject> listo = cursor.sort(dbObject).toArray();
		List<Map> listMap = new ArrayList<Map>();
		if (null != listo) {
			Map temp = null;
			for (int i = 0; i < listo.size(); i++) {
				DBObject dbo = listo.get(i);
				Iterator<String> it = dbo.keySet().iterator();
				temp = new HashMap();
				while (it.hasNext()) {
					String key = it.next();
					temp.put(key, dbo.get(key));
				}
				listMap.add(temp);
			}
		}
		return listMap;
	}

	/**
	 * 查询GridFS文件列表
	 * @author 孟飞 
	 * @param id
	 * @param sort
	 * @param value
	 * @return
	 * @throws Exception List<Map>
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public List<Map> queryGridFSById(String id, int sort, String value, String modelName) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.queryGridFSByMap(map, sort, value, modelName);
	}

	/**
	 * 查询GridFS文件列表
	 * @author 孟飞 
	 * @param map
	 * @param sort
	 * @param value
	 * @return
	 * @throws Exception List<Map>
	 * @throws
	 */
	@SuppressWarnings({"rawtypes", "unchecked"})
	public List<Map> queryGridFSByMap(Map<String, ?> map, int sort, String value, String modelName) throws Exception {
		if (this.myFSMap.get(modelName) == null) {
			return this.queryGridFSByMap(map, sort, value);
		}

		BasicDBObject searchGridFS = new BasicDBObject();// 可选项-作为查询条件使用
		DBCursor cursor = null;
		if (null != map) {
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				if (key.equals("_id")) {
					searchGridFS.put(key, new ObjectId(map.get(key).toString()));
				} else {
					searchGridFS.append(key, map.get(key));
				}
			}
			cursor = this.myFSMap.get(modelName).getFileList(searchGridFS);
		} else {
			cursor = this.myFSMap.get(modelName).getFileList();
		}
		DBObject dbObject = new BasicDBObject();
		if (null != value && !"".equals(value) && (value.equals("uploadDate") || value.equals("filename"))) {
			dbObject.put(value, sort);
		}
		List<DBObject> listo = cursor.sort(dbObject).toArray();
		List<Map> listMap = new ArrayList<Map>();
		if (null != listo) {
			Map temp = null;
			for (int i = 0; i < listo.size(); i++) {
				DBObject dbo = listo.get(i);
				Iterator<String> it = dbo.keySet().iterator();
				temp = new HashMap();
				while (it.hasNext()) {
					String key = it.next();
					temp.put(key, dbo.get(key));
				}
				listMap.add(temp);
			}
		}
		return listMap;
	}

	/**
	 * 查询GridFS文件列表
	 * @author 孟飞 
	 * @param id
	 * @return
	 * @throws Exception List<Map>
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public List<Map> queryGridFSById(String id) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.queryGridFSByMap(map);
	}

	/**
	 * 查询GridFS文件列表
	 * @author 孟飞 
	 * @param map
	 * @return
	 * @throws Exception List<Map>
	 * @throws
	 */
	@SuppressWarnings({"rawtypes", "unchecked"})
	public List<Map> queryGridFSByMap(Map<String, ?> map) throws Exception {

		BasicDBObject searchGridFS = new BasicDBObject();// 可选项-作为查询条件使用
		DBCursor cursor = null;
		if (null != map) {
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				if (key.equals("_id")) {
					searchGridFS.append(key, new ObjectId(map.get(key).toString()));
				} else {
					searchGridFS.append(key, map.get(key));
				}
			}
			cursor = myFS.getFileList(searchGridFS);
		} else {
			cursor = myFS.getFileList();
		}
		DBObject dbObject = new BasicDBObject();
		dbObject.put("uploadDate", -1);
		List<DBObject> listo = cursor.sort(dbObject).toArray();
		List<Map> listMap = new ArrayList<Map>();
		if (null != listo) {
			Map temp = null;
			for (int i = 0; i < listo.size(); i++) {
				DBObject dbo = listo.get(i);
				Iterator<String> it = dbo.keySet().iterator();
				temp = new HashMap();
				while (it.hasNext()) {
					String key = it.next();
					temp.put(key, dbo.get(key));
				}
				listMap.add(temp);
			}
		}
		return listMap;
	}

	/**
	 * 查询GridFS文件列表
	 * @author 孟飞 
	 * @param id
	 * @return
	 * @throws Exception List<Map>
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public List<Map> queryGridFSById(String id, String modelName) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.queryGridFSByMap(map, modelName);
	}

	/**
	 * 查询GridFS文件列表
	 * @author 孟飞 
	 * @param map
	 * @return
	 * @throws Exception List<Map>
	 * @throws
	 */
	@SuppressWarnings({"rawtypes", "unchecked"})
	public List<Map> queryGridFSByMap(Map<String, ?> map, String modelName) throws Exception {
		if (this.myFSMap.get(modelName) == null) {
			return this.queryGridFSByMap(map);
		}

		BasicDBObject searchGridFS = new BasicDBObject();// 可选项-作为查询条件使用
		DBCursor cursor = null;
		if (null != map) {
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				if (key.equals("_id")) {
					searchGridFS.append(key, new ObjectId(map.get(key).toString()));
				} else {
					searchGridFS.append(key, map.get(key));
				}
			}
			cursor = this.myFSMap.get(modelName).getFileList(searchGridFS);
		} else {
			cursor = this.myFSMap.get(modelName).getFileList();
		}
		DBObject dbObject = new BasicDBObject();
		dbObject.put("uploadDate", -1);
		List<DBObject> listo = cursor.sort(dbObject).toArray();
		List<Map> listMap = new ArrayList<Map>();
		if (null != listo) {
			Map temp = null;
			for (int i = 0; i < listo.size(); i++) {
				DBObject dbo = listo.get(i);
				Iterator<String> it = dbo.keySet().iterator();
				temp = new HashMap();
				while (it.hasNext()) {
					String key = it.next();
					temp.put(key, dbo.get(key));
				}
				listMap.add(temp);
			}
		}
		return listMap;
	}

	/**
	 * 删除文件操作
	 * @author 孟飞 
	 * @param id
	 * @return int
	 * @throws
	 */
	public int deleteGridFS(String id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.deleteGridFS(map);
	}

	/**
	 * 删除文件操作
	 * @author 孟飞 
	 * @param map
	 * @return int
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public int deleteGridFS(Map map) {
		try {
			DBObject o = new BasicDBObject();
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				o.put(key, map.get(key));
			}
			myFS.remove(o);
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 删除文件操作
	 * @author 孟飞 
	 * @param id
	 * @return int
	 * @throws
	 */
	public int deleteGridFS(String id, String modelName) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(this.keyfiled, id);
		return this.deleteGridFS(map, modelName);
	}

	/**
	 * 删除文件操作
	 * @author 孟飞 
	 * @param map
	 * @return int
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public int deleteGridFS(Map map, String modelName) {
		if (this.myFSMap.get(modelName) == null) {
			this.deleteGridFS(map);
		}

		try {
			DBObject o = new BasicDBObject();
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				o.put(key, map.get(key));
			}
			this.myFSMap.get(modelName).remove(o);
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 关闭数据库连接
	 * @author 孟飞 
	 * @throws Exception void
	 * @throws
	 */
	public void close() throws Exception {
		this.mongo.close();
	}

	public String getKeyfiled() {
		return keyfiled;
	}

	/**
	 * 插入加密文件操作
	 * @param inputStream
	 * @param filePathName
	 * @return 1 成功，0失败
	 */
	public int insertAes128GridFS(InputStream inputStream, String filePathName, Map<String, String> map, String sKey) {
		try {
			GridFSInputFile gridFSInputFile = myFS.createFile(filePathName);
			gridFSInputFile.setChunkSize(1024 * 1024 * 2);// 设置每次-设置为2M这样可以减少生成的collection内document的数量
			// gridFSInputFile.put("aliases", id);
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				gridFSInputFile.put(key, map.get(key));
			}

			Cipher cipher = AESUtil.init128AESCipher(sKey, Cipher.ENCRYPT_MODE);
			CipherInputStream cin = new CipherInputStream(inputStream, cipher);
			byte[] buff = new byte[buffsize];
			int bytes = 0;
			while (-1 != (bytes = cin.read(buff))) {
				gridFSInputFile.getOutputStream().write(buff, 0, bytes);
			}

			gridFSInputFile.getOutputStream().close();

			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
}
